﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO.Pipes;

namespace InterprocessCommunicationTesting
{
    public partial class ServerForm : Form
    {
        NamedPipeServerStream m_PipeServer;
        const string m_PipeName = "ThisIsATestPipe";

        public ServerForm()
        {
            InitializeComponent();

            // If server is run as admin and client is not...
            //PipeSecurity pipeSecurity = new PipeSecurity();
            //pipeSecurity.AddAccessRule(new PipeAccessRule("Everyone",
            //    PipeAccessRights.FullControl,
            //    System.Security.AccessControl.AccessControlType.Allow));
            //m_PipeServer =
            //    new NamedPipeServerStream(
            //        m_PipeName,
            //        PipeDirection.In,
            //        1,
            //        PipeTransmissionMode.Message,
            //        PipeOptions.Asynchronous,
            //        m_BufferSize,
            //        m_BufferSize,
            //        pipeSecurity);

            m_PipeServer =
                new NamedPipeServerStream(m_PipeName, PipeDirection.In, 1, PipeTransmissionMode.Message, PipeOptions.Asynchronous);
            m_PipeServer.BeginWaitForConnection(BeginWaitForConnection, null);

            //String[] listOfPipes = System.IO.Directory.GetFiles(@"\\.\pipe\");
        }

        void BeginWaitForConnection(IAsyncResult result)
        {
            m_PipeServer.EndWaitForConnection(result);

            if (m_PipeServer.IsConnected)
            {
                m_PipeServer.BeginRead(m_Buffer, 0, m_Buffer.Length, new AsyncCallback(BeginRead), null);
            }
        }

        byte[] m_Buffer = new byte[1024];
        void BeginRead(IAsyncResult result)
        {
            int length = m_PipeServer.EndRead(result);

            List<byte> myList = new List<byte>();
            for (int i = 0; i < length; i++)
            {
                myList.Add(m_Buffer[i]);
            }

            if (length > 0)
            {
                while (!m_PipeServer.IsMessageComplete)
                {
                    length = m_PipeServer.Read(m_Buffer, 0, m_Buffer.Length);

                    for (int i = 0; i < length; i++)
                    {
                        myList.Add(m_Buffer[i]);
                    }
                }

                System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();
                string str = enc.GetString(myList.ToArray());
                //this.Invoke(new Action(() => { this.Activate(); }));
                this.Invoke(new Action(() => { textBox1.Text = str; }));
            }

            //m_PipeServer.BeginRead(m_Buffer, 0, m_Buffer.Length, new AsyncCallback(BeginRead), null);
            m_PipeServer.Disconnect();
            m_PipeServer.BeginWaitForConnection(BeginWaitForConnection, null);
        }
    }
}
